﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OpenSearchHandler.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   Open Search Handler
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Api.HttpHandlers
{
    using System;
    using System.Text;
    using System.Web;
    using System.Xml;

    using EcBlog.Data;
    using EcBlog.Models;

    /// <summary>
    /// Open Search Handler
    /// </summary>
    public class OpenSearchHandler : IHttpHandler
    {
        private readonly IEcUnitOfWork unitOfWork;

        /// <summary>
        /// Initializes a new instance of the <see cref="OpenSearchHandler"/> class.
        /// </summary>
        public OpenSearchHandler()
        {
            this.unitOfWork =
                Composition.CompositionDependencyResolver.StaticFactory.GetService(typeof(IEcUnitOfWork)) as
                IEcUnitOfWork;
        }

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            var settings = new XmlWriterSettings { Indent = true };

            using (var writer = XmlWriter.Create(context.Response.OutputStream, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("OpenSearchDescription", "http://a9.com/-/spec/opensearch/1.1/");
                writer.WriteAttributeString("xmlns", "http://a9.com/-/spec/opensearch/1.1/");

                writer.WriteElementString("ShortName", this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteTitle]);
                writer.WriteElementString("Description", this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteDescription]);

                writer.WriteStartElement("Image");
                writer.WriteAttributeString("height", "16");
                writer.WriteAttributeString("width", "16");
                writer.WriteAttributeString("type", "image/vnd.microsoft.icon");
                writer.WriteValue(string.Format("{0}/Content/favicon.ico", this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl]));
                writer.WriteEndElement();

                writer.WriteStartElement("Url");
                writer.WriteAttributeString("type", "text/html");
                writer.WriteAttributeString("template", string.Format("{0}/Blog/Search?searchKey={{searchTerms}}", this.unitOfWork.SiteSettingRepository[SiteSettingKey.SiteUrl]));
                writer.WriteEndElement();

                writer.WriteEndElement();
            }

            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentType = "text/xml";
            context.Response.Charset = "UTF-8";
            context.Response.Cache.SetValidUntilExpires(true);
            context.Response.Cache.SetExpires(DateTime.Now.AddDays(30));
            context.Response.Cache.SetETag(Guid.NewGuid().ToString());
        }
    }
}